package com.p2p.front.dal.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.p2p.base.dao.BaseMapper;
import com.p2p.base.model.Money;
import com.p2p.base.page.PageVo;
import com.p2p.front.dal.model.TenderReturn;

/**
 * @author ruiqi
 *
 */
public interface TenderReturnDao extends BaseMapper {

	String column_names = "id,tid,no,uid,lid,principal,amount,time,start_time,status,is_flow,return_type,extra_interest,updateTime";
	
	void create(TenderReturn tenderReturn);
	
	List<Map<String, Object>> listPage(PageVo form);
	List<Map<String, Object>> listPageRecycled(PageVo form);
	
	List<TenderReturn> queryByUserCondition(TenderReturn tenderReturn);
	
	int update(TenderReturn tenderReturn);
	
	int batchDeleteByIdList(List<Long> idList);
	
	
	@ResultMap(value="TenderReturnResultMap")
	@Select("select "+column_names+" from p2p_tender_return where lid=#{lid} and no=#{no}")
	List<TenderReturn> queryByLidAndNo(@Param(value = "lid")Long lid,@Param(value = "no")Integer no);
	
	@ResultMap(value="TenderReturnResultMap")
	@Select("select "+column_names+" from p2p_tender_return where lid=#{lid} and no=#{no} and status=#{status}")
	List<TenderReturn> queryByLidAndNoAndStatus(@Param(value = "lid")Long lid,@Param(value = "no")Integer no,@Param(value = "status")Integer status);
	
	
	@Update("update p2p_tender_return set status=#{status} where id=#{id} and status=#{oldStatus}")
	int updateStatus(@Param(value = "id")Long id,@Param(value = "oldStatus")Integer oldStatus,@Param(value = "status")Integer status);
	
	@Update("update p2p_tender_return set status=31,return_type=#{returnType},extra_interest=#{extraInterest.cent},updateTime=now() where id=#{id} and status=21")
	int updateStatusForReturn(@Param(value = "id")Long id,@Param(value="returnType")Integer returnType, @Param(value="extraInterest") Money extraInterest);
	
	
	//@ResultMap(value="TenderReturnResultMap")
	//@Select("select "+column_names+" from p2p_tender_return where time between #{startTime} and #{endTime} and isFlow=#{isFlow}")	
	List<TenderReturn> queryAllByTime(@Param("startTime")Date startTime,@Param("endTime")Date endTime,@Param("isFlow")Integer isFlow,@Param("status") Integer status);
	
	@Select("select sum(amount-principal) from p2p_tender_return where status > 30")
	long getEarnings(Date lastUpdateTime);
	
	@Select("select sum(amount-principal) from p2p_tender_return where status = 31 "
			+ "and updateTime between #{dateBegin} and #{dateEnd} and amount-principal>0")
	Long getDayEarnings(@Param("dateBegin")Date dateBegin, @Param("dateEnd")Date dateEnd);
	
	@Select("select "+column_names+" from p2p_tender_return where "
			+ " time between #{startDate} and #{endDate}")
	List<TenderReturn> queryByTime(PageVo form);
	
	@Select("select * from p2p_tender_return where uid=#{uid}")
	List<Map<String, Object>> listAll(@Param("uid")Integer uid);
}
